#region Librerias

using System;
using ESF.General.GUI;
using Gtk;

#endregion


namespace ESF.GTK
{
	public partial class ProgressDialog : Gtk.Dialog, IProgressDialog<Gtk.Window>
	{
		#region Miembros

		private bool _WasCancelled;

		public event System.Action OnCancel;

		#endregion


		#region Constructor
		
		public ProgressDialog ()
		{
			this.Build ();

			HideWhenComplete = true;
			IsCancelable = false;

			imgIcon.Pixbuf = GTKConverter.ImageToPixbuf(ESF.GTK.Resources.Clock_64x64);
		}

		#endregion


		#region Methods
		
		public void Show (Window pParent)
		{
			base.Show ();
			
			//base.CenterToParent ();
		}
		
		
		public void Clear()
		{
			btnOK.Sensitive = false;
			btnCancel.Sensitive = false;
			
			_WasCancelled = false;
			
			GTKHelper.ClearAdvance (lblCurrentStepName, prgProgressBar);			
		}
		
		#endregion


		#region Button Events

		protected void OnBtnCancelClicked (object sender, EventArgs e)
		{
			if (OnCancel != null)
			{
				OnCancel ();
			}
			
			Hide ();
		}


		protected void OnBtnOKClicked (object sender, EventArgs e)
		{
			if (prgProgressBar.Fraction == 100)
			{
				Hide ();
			}
		}

		#endregion


		#region Window Events
		
		public new void Hide ()
		{
			Gtk.Application.Invoke (delegate { base.Hide (); });
		}


		protected void OnDeleteEvent (object o, DeleteEventArgs args)
		{
			args.RetVal = true;
			
			if (prgProgressBar.Fraction == 100)
			{
				Hide ();
			}
		}

		#endregion


		#region Interface Fields
		
		public bool WasCancelled
		{
			get { return _WasCancelled; }
		}
		
		public bool HideWhenComplete { set; get; }
		
		
		public bool IsCancelable
		{
			set { btnCancel.Sensitive = value; }
			get { return btnCancel.Sensitive; }
		}
		
		public double PercentageAdvance100
		{
			set 
			{ 
				GTKHelper.SetFraction (prgProgressBar, value); 
				
				if (value == 100)
				{
					Application.Invoke(delegate { 					        
						btnOK.Sensitive = true;
						btnOK.HasFocus = true;
						btnCancel.Sensitive = false;

						if (HideWhenComplete)
						{
							Hide ();
						}
					});
				}
			}
		}
		
		public new string Title
		{
			set { GTKHelper.SetTitle (this, value); }
		}
		
		public string CurrentStepName
		{
			set { GTKHelper.SetText (lblCurrentStepName, value); }
		}
		
		#endregion
	}
}

